package com.pafcio.sudoku;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.fest.util.Collections;

public class Area {

	private Integer[] values = new Integer[9];
	
	public Area(Integer[][] integers) {
		this.values[0] = integers[0][0];
		this.values[1] = integers[0][1];
		this.values[2] = integers[0][2];
		this.values[3] = integers[1][0];
		this.values[4] = integers[1][1];
		this.values[5] = integers[1][2];
		this.values[6] = integers[2][0];
		this.values[7] = integers[2][1];
		this.values[8] = integers[2][2];
	}

	public boolean isOK() {		
		return containsAllDigits();
	}

	private boolean containsAllDigits() {
		Set<Integer> numbersSet = Collections.set(values);
		for(int i=1; i<=9; i++){
			if(!numbersSet.contains(Integer.valueOf(i)))
				return false;
		}
		
		return true;
	}

	public Integer[] getRow(int rowNo) {
		int from = rowNo*3;
		int to = rowNo*3 + 3;
		
		return Arrays.copyOfRange(values, from, to);
	}

	public Integer[] getColumn(int columnNo) {
		Integer[] result = new Integer[3];
				
		result[0] = values[columnNo + 0*3];
		result[1] = values[columnNo + 1*3];
		result[2] = values[columnNo + 2*3];
		
		return result;
	}

	public void setValueAt(int rowX, int colX, Integer value) {
		values[3* rowX + colX] = value;		
	}

	public boolean containsNumber(int i) {
		return Arrays.asList(values).contains(i);
	}

	public Set<Integer> getMissingValues() {
		Set<Integer> missingValues = new HashSet<Integer>();
		for(int i = 1; i <= 9; i++) {
			missingValues.add(i);
		}
		
		missingValues.removeAll(Arrays.asList(values));
		
		return missingValues;
	}

	public Set<Integer> getMissingIndexes() {
		Set<Integer> missingIndex = new HashSet<Integer>();
		for(int i = 1; i <= 9; i++) {
			if(values[i-1] == null)
				missingIndex.add(i-1);
		}
		
		return missingIndex; 
	}
}
